<template>
  <div>
    <el-dialog
      :title="dialogTitle"
      :visible="visible"
      width="50%"
      @close="onClose"
    >

      <el-form ref="form" :model="addDeptFrom" :rules="addDeptFromRules" label-width="80px">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="addDeptFrom.name" placeholder="请输入部门名称" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="addDeptFrom.code" placeholder="请输入部门名称" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="addDeptFrom.manager" placeholder="请选择活动区域" @focus="getList">
            <el-option v-for="item in list" :key="item.id" :label="item.username" :value="item.username" />
          </el-select>
        </el-form-item>
        <el-form-item label="部门接收" prop="introduce">
          <el-input
            v-model="addDeptFrom.introduce"
            placeholder="请输入部门名称"
          />
        </el-form-item>
      </el-form>

      <span>这是一段信息</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="$emit('input',false)">取 消</el-button>
        <el-button
          type="primary"
          @click="onSave"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getDepartmentInfo, addDepartment, getDepartmentInfoById, editDepartmentById } from '@/api/department'
import { getEmployeesList } from '@/api/employees'
export default {
  model: {
    prop: 'visible'
  },
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    node: {
      type: Object,
      required: true
    }
  },
  data() {
    // 校验名称
    var validatePass = async(rule, value, callback) => {
      let flag = false
      if (this.addDeptFrom.id) {
        // 编辑
        const res = await getDepartmentInfo()
        console.log(res)
        const newArr = res.depts.filter(item => item.pid !== this.node.pid)
        flag = newArr.some(item => item.name === value)
      } else {
        // 新增
        if (!this.node.children) return callback()
        flag = this.node.children.some(item => item.name === value)
      }
      if (flag) {
        callback(new Error(value + '已存在'))
      } else {
        callback()
      }
    }
    // 校验编码
    var dateIntroduce = async(rule, value, callback) => {
      console.log(value)
      // if (!this.node.children) return callback()
      const res = await getDepartmentInfo()
      let flag = false
      if (this.addDeptFrom.id) {
        // 编辑
        const newArr = res.depts.filter(item => item.code !== this.node.code)
        flag = newArr.some(item => item.code === value)
      } else {
        // 新增
        flag = res.depts.some(item => item.code === value)
      }
      if (flag) return callback(new Error(value + '已存在'))
      callback()
    }
    return {
      addDeptFrom: {
        name: '',
        manager: '',
        code: '',
        introduce: ''
      },
      list: [],
      addDeptFromRules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 10, message: '部门名称长度1-10位', trigger: 'blur' },
          {
            validator: validatePass, trigger: 'blur'
          }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 1, max: 10, message: '请输入部门编码长度1-10位', trigger: 'blur' },
          {
            validator: dateIntroduce, trigger: 'blur'
          }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 50, message: '请输入部门介绍长度1-50位', trigger: 'blur' }
        ]

      }
    }
  },
  computed: {
    dialogTitle() {
      return this.addDeptFrom.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
  },

  methods: {
    onClose() {
      this.$emit('input', false)
      this.$refs.form.resetFields()
      this.addDeptFrom = {
        name: '',
        manager: '',
        code: '',
        introduce: ''
      }
    },
    async getList() {
      const res = await getEmployeesList()
      this.list = res
    },
    async onSave() {
      try {
        await this.$refs.form.validate()
        if (this.addDeptFrom.id) {
          console.log(123)
          editDepartmentById(this.addDeptFrom)
          this.$message.success('修改成功')
          this.$emit('edit-success')
        } else {
          this.addDeptFrom.pid = this.node.id
          await addDepartment(this.addDeptFrom)
          this.$message.success('添加成功')
          this.$emit('add-success')
        }
      } catch (err) {
        console.log(err)
      }
      // this.$emit('input', false)
    },
    async getDeptInfo(id) {
      const res = await getDepartmentInfoById(id)
      console.log(res)
      this.addDeptFrom = res
    }
  }
}
</script>

<style scoped lang='less'>

</style>
